<template>
	<view class="pricing">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				计费策略
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}">

		</view>
		<view class="headtitles">修改门店策略</view>
		<view class="form" v-for="(item,index) in chargerLine" :key="index">
			<view class="content clearfix">
				<view class="name fl">时间(小时)</view>
				<view class="inp fr">
					<input type="text" placeholder="时间" v-model="item.time"/>
				</view>
			</view>
			<view class="content clearfix">
				<view class="name fl">单价(元)</view>
				<view class="inp fr">
					<input type="text" placeholder="单价" v-model="item.money"/>
				</view>
			</view>
			<view class="content contentlast clearfix">
				<view class="name fl"></view>
				<view class="inp fr">
					<input type="text" placeholder="单价" disabled value="删除"  @click="del(index)"/>
				</view>
			</view>
		</view>
		<view class="xinzeng" @click="add">新增策略</view>
		
		<view class="footer">
			<view class="submit" @click="submit1">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				stationid: '',
				killingform:{
					mianfei: "",//免费时长
					minute: '',//时间
					danjia: '',//价格
					dayconsume: 0.00,//每日封顶
					maxmoney: '99',//封顶金额
					attr: 1,//1分钟 2小时
					type: 1,
					stationid: '',
					agent_id: uni.getStorageSync('agent_id'),
					line_combo: {
						
					}
				},
				formData:{},
				chargerLine: [],
			}
		},
		onLoad(option) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			this.stationid = option.stationid;

			//获取门店的价格策略
			this.billingInfo();
		},
		methods: {
			billingInfo(){
				// console.log("asdfsadf");
				this.request({
					url: '/agent/billinginfo',
					data: {stationid: this.stationid},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.killingform.mianfei = res.data.data.billingdata.mianfei;
						this.killingform.minute = res.data.data.billingdata.minute;
						this.killingform.danjia = res.data.data.billingdata.danjia;
						this.killingform.dayconsume = res.data.data.billingdata.dayconsume;
						this.killingform.maxmoney = res.data.data.billingdata.maxmoney;
						this.killingform.line_combo = res.data.data.billingdata.line_combo;
						this.killingform.attr = res.data.data.billingdata.attr;
						
						this.chargerLine = JSON.parse(this.killingform.line_combo)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			submit1(){
				//创建计费规则
				//判断一下是提交的哪个
				let xqLIst = [];
				for (let i = 0; i < this.chargerLine.length; i++) {
					if (this.chargerLine[i].time && this.chargerLine[i].money) {
						xqLIst.push({
							time: this.chargerLine[i].time,
							money: this.chargerLine[i].money,
						})
					}
				}
				this.killingform.line_combo = JSON.stringify(xqLIst);
				
				var formData = {};
				formData = this.killingform;
				
				formData.stationid = this.stationid;
				
				this.request({
					url: '/agent/editbilling',
					data: formData,
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						if(formData.type==1){//普通充电宝
							var id = res.data.data;
							uni.showToast({
								title: '保存成功！',
								icon: 'none'
							})
							setTimeout(function(){
								uni.navigateBack({
								    //关闭当前页面，返回上一页面或多级页面。
								    delta:1
								});
							},2000)
						}else{//快充
							var id = res.data.data;
							uni.showToast({
								title: '保存成功！',
								icon: 'none'
							})
							setTimeout(function(){
								uni.navigateBack({
								    //关闭当前页面，返回上一页面或多级页面。
								    delta:1
								});
							},2000)
						}
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			add() {
				this.chargerLine.push({
					time: '',
					money: '',
				});
			},
			del(index) {
				this.chargerLine.splice(index, 1);
			},
			fanhui() {
				uni.navigateBack();
			},
			topage(url) {
				uni.navigateTo({
					url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.pricing {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			padding: 20rpx 40rpx 0;
			box-sizing: border-box;

			.left {
				.mess {
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;

					view {
						font-size: 24rpx;
						color: #333333;
					}

					.dian {
						width: 8rpx;
						height: 8rpx;
						background: #5C88FA;
						display: block;
						border-radius: 50%;
						margin: 0 auto;
					}

					.active {
						font-size: 32rpx;
						color: #333333;
						font-weight: 700;
						margin-left: 0;
					}
				}

				.mess1 {
					margin-left: 20rpx;
				}
			}
		}
		
		.nr{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: -200rpx;
			
			.headtitle{
				font-size: 28rpx;
				font-weight: 700;
				margin-bottom: 20rpx;
				color: #333333;
				text-align: center;
			}
			
			.list{
				width: 100%;
				background: #fff;
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				padding: 40rpx 40rpx 10rpx;
				position: relative;
				box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
				
				.liebiao{
					margin-bottom: 30rpx;
					
					.left{
						font-size: 24rpx;
						color: #333333;
						width: 36%;
					}
					
					.right{
						
						.title{
							font-size: 24rpx;
							color: #333333;
						}
						
						.name{
							font-size: 24rpx;
							color: #5C88FA;
						}
					}
				}
			}
		}
		
		.headtitles{
			font-size: 28rpx;
			font-weight: 700;
			margin-bottom: 20rpx;
			margin-top: -280rpx;
			color: #333333;
			text-align: center;
		}
		
		.form{
			width: 90%;
			margin: 0 auto;
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 0 20rpx;
			box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.content{
				height: 92rpx;
				line-height: 92rpx;
				border-bottom: 1rpx solid #D8D8D8;
				
				.name{
					font-size: 28rpx;
					color: #333333;
					width: 40%;
				}
				
				.inp{
					width: 60%;
					text-align: right;
					
					input{
						font-size: 28rpx;
						color: #999999;
						height: 92rpx;
						line-height: 92rpx;
					}
				}
				
				.xuanze{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					color: #368BF8;
				}
				
				.img{
					width: 60%;
					text-align: right;
					height: 92rpx;
					line-height: 92rpx;
					
					image{
						width: 108rpx;
						height: 64rpx;
						margin-top: 12rpx;
					}
				}
			}
			
			.contentlast{
				border: none;
			}
		}
		
		.xinzeng{
			width: 90%;
			margin: 0 auto;
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #368BF8;
		}
		
		.footer{
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 60rpx;
			height: 72rpx;
			text-align: center;
			
			.submit{
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-left: 248rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>